<?php

/**
 * precioComb actions.
 *
 * @package    aeropostal
 * @subpackage precioComb
 * @author     Your name here
 */
class precioCombActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $pager = new sfPropelPager('precioComb', sfConfig::get('app_max_preciocomb'));
	$pager->setCriteria(new Criteria());
	// Se establece que la pagina principal del index sera la 1
	$pager->setPage($this->getRequestParameter('page', 1));
	$pager->init();
	 // Se Prepara la informaci�n para usarla en la vista.
	$this->pager = $pager;
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->PrecioComb = PreciocombPeer::retrieveByPk($request->getParameter('fecha1'),
                             $request->getParameter('proveedor'),
                             $request->getParameter('estacion'),
                             $request->getParameter('tipoComb'));
    $this->forward404Unless($this->PrecioComb);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PrecioCombForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PrecioCombForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($PrecioComb = PreciocombPeer::retrieveByPk($request->getParameter('fecha1'),
                       $request->getParameter('proveedor'),
                       $request->getParameter('estacion'),
                       $request->getParameter('tipoComb')), sprintf('Object PrecioComb does not exist (%s).', $request->getParameter('fecha1'),
                       $request->getParameter('proveedor'),
                       $request->getParameter('estacion'),
                       $request->getParameter('tipoComb')));
    $this->form = new PrecioCombForm($PrecioComb);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($PrecioComb = PreciocombPeer::retrieveByPk($request->getParameter('fecha1'),
                       $request->getParameter('proveedor'),
                       $request->getParameter('estacion'),
                       $request->getParameter('tipoComb')), sprintf('Object PrecioComb does not exist (%s).', $request->getParameter('fecha1'),
                       $request->getParameter('proveedor'),
                       $request->getParameter('estacion'),
                       $request->getParameter('tipoComb')));
    $this->form = new PrecioCombForm($PrecioComb);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($PrecioComb = PreciocombPeer::retrieveByPk($request->getParameter('fecha1'),
                       $request->getParameter('proveedor'),
                       $request->getParameter('estacion'),
                       $request->getParameter('tipoComb')), sprintf('Object PrecioComb does not exist (%s).', $request->getParameter('fecha1'),
                       $request->getParameter('proveedor'),
                       $request->getParameter('estacion'),
                       $request->getParameter('tipoComb')));
    $PrecioComb->delete();
    $numero = $request->getParameter('fecha1').'-'.$request->getParameter('proveedor').'-'.$request->getParameter('estacion').'-'.$request->getParameter('tipoComb');
    LogPeer::logBorrar($numero,$this->getUser(),'PrecioComb');
    $this->redirect('precioComb/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
        try {
            $PrecioComb = $form->save();
            $numero = $request->getPostParameter('preciocomb[fecha1]').'-'.$request->getPostParameter('preciocomb[proveedor]').'-'.$request->getPostParameter('preciocomb[estacion]').'-'.$request->getPostParameter('preciocomb[tipoComb]');
            LogPeer::log($numero,$this->getUser(),'PrecioComb');
            $this->redirect('precioComb/index');
        }catch (Exception $e){
            $this->getUser()->setFlash('error', 'No se puede registrar el precio de combustible, ya existe un monto para ese combustible en esa estación.');
            return sfView::NONE;
        }
    }
  }
  
  public function executeMostrarMonedaUMedida(sfWebRequest $request)
  {
            $reclamo = $request->getParameter('proveedor');
            $respuesta = ProveedorPeer::doSelectMonedaUMedidaByRif($reclamo);
            $this->getResponse()->setContent($respuesta);
            return sfView::NONE;

  }
}
